package com.bookshop.util;

import java.security.MessageDigest;
import java.util.Base64;

public class MD5Util {
    public static final String KEY_MD5 = "MD5";
    public static final Integer DESIRED_LENGTH = 16;

    // MD5加密
    public static String getPassword(String password) {
        String result = null;
        try {
            MessageDigest md = MessageDigest.getInstance(KEY_MD5);
            byte[] inputData = password.getBytes();
            // 更新消息摘要
            md.update(inputData);
            // 进行Base64编码
            result = Base64.getEncoder().encodeToString(md.digest());
            // 截断到固定长度
            result = result.substring(0, DESIRED_LENGTH);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 长度为16
        return result;
    }
}